//
//  regist.m
//  Project2
//
//  Created by hundredsandc on 2017/11/8.
//  Copyright © 2017年 hundredsandc. All rights reserved.
//

#import "registVC.h"

@interface registVC ()<UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UIButton *registBtn;
@property (weak, nonatomic) IBOutlet UITextField *userNameTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF;
@property (weak, nonatomic) IBOutlet UITextField *passWordTF2;

@end

@implementation registVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    _registBtn.layer.cornerRadius = 20.0f;
    _registBtn.layer.masksToBounds = YES;
    [_userNameTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    [_passWordTF addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
    [_passWordTF2 addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(resignTF)];
    [self.view addGestureRecognizer:tap];
    
}
-(void)resignTF{
    [_userNameTF resignFirstResponder];
    [_passWordTF resignFirstResponder];
    [_passWordTF2 resignFirstResponder];
    
}
-(void)textFieldDidBeginEditing:(UITextField *)textField{
    textField.text=@"";
    if (textField==_userNameTF) {
        if (![textField.text isEqualToString:@"请输入账号"]) {
            
        }
    }
    if (textField==_passWordTF) {
        textField.secureTextEntry = YES;
        
    }
    if (textField==_passWordTF2) {
        textField.secureTextEntry = YES;
        
    }
}
-(void)textFieldDidEndEditing:(UITextField *)textField{
    if (textField==_userNameTF) {
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"请输入账号";
        }
    }
    if (textField==_passWordTF) {
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"请输入密码";
            textField.secureTextEntry = NO;
        }
    }
    if (textField==_passWordTF2) {
        if ([textField.text isEqualToString:@""]) {
            textField.text = @"请再次输入密码";
            textField.secureTextEntry = NO;
        }
    }
}
-(void)textFieldDidChange:(UITextField *)theTextField{
  
}

- (IBAction)registClick:(id)sender {
    if (_userNameTF.text == nil || [_userNameTF.text removeWhitespaceAndNewlinewithboolNewLine:YES].length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入用户名"];
        return;
    }
    if (_passWordTF.text.length < 6) {
        [SVProgressHUD showInfoWithStatus:@"密码至少6位"];
        return;
    }
    if (![_passWordTF2.text isEqualToString:_passWordTF.text ]) {
        [SVProgressHUD showInfoWithStatus:@"两次输入的密码不一样"];
        return;
    }
    
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"username"] = _userNameTF.text;
    params[@"password"] = _passWordTF.text;
    
    [HTTPManagers postForumUserWithURL:@"reg" params:params success:^(id json) {
        if ([json[@"message"]isEqualToString:@"success"]) {
            
            [SVProgressHUD showSuccessWithStatus:@"注册成功" maskType:SVProgressHUDMaskTypeBlack];
            [self performSelector:@selector(pop) withObject:nil afterDelay:1];
            
        }else{
            
            [SVProgressHUD showErrorWithStatus:@"注册失败" maskType:SVProgressHUDMaskTypeBlack];
        }
    } failure:^(NSError *error) {
        
    }];
    
}
-(void)pop{
    [self.navigationController popViewControllerAnimated:YES];
}
@end
